package org.springblade.modules.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.utils.SqlInjectionUtils;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.system.entity.Tenant;
import org.springblade.modules.system.entity.TenantConfig;
import org.springblade.modules.system.service.ITenantService;
import org.springblade.modules.system.service.TenantConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.regex.Pattern;

/**
 * @Date 10:26 2023/4/6
 * @Description {
 *   租户业务配置
 * }
 * @Author lsay
 **/
@RestController
@RequestMapping("/tenant/config")
public class TenantConfigController {

	@Autowired
	private TenantConfigService tenantConfigService;
	@Autowired
	private ITenantService tenantService;

	/**
	 * @Author lsay
	 * @Description {
	 *   查询租户业务配置
	 * }
	 * @Date 10:48 2023/4/6
	 * @return
	 **/
	@GetMapping()
	public R<TenantConfig> getTenantConfig(String tenantId){
		String finalTenantId = Func.isNotEmpty(tenantId) ? tenantId : AuthUtil.getTenantId();
		if(Func.isEmpty(finalTenantId)){
			throw new ServiceException("非法传参请求");
		}
		if(SqlInjectionUtils.checkV2(finalTenantId)){
			throw new ServiceException("非法传参请求");
		}
		long count = tenantService.count(Wrappers.<Tenant>lambdaQuery().eq(Tenant::getTenantId,finalTenantId));
		if(count <= 0){
			throw new ServiceException("租户不存在");
		}
		return R.data(tenantConfigService.getTenantConfig(finalTenantId));
	}

}
